package com.gt.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gt.common.core.controller.BaseController;
import com.gt.common.core.domain.AjaxResult;
import com.gt.admin.domain.City;
import com.gt.admin.service.ICityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 城市Controller
 */
@RestController
@RequestMapping("/city")
@Api(tags = "sc-城市")
public class CityController extends BaseController {
    @Autowired
    private ICityService cityService;

    /**
     * 查询所有层次树状城市信息
     */
    public List<City> getCityMenu(Long parentId){
        LambdaQueryWrapper<City> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(City::getParentId, parentId);
        List<City> list = cityService.list(queryWrapper);
        for (City city: list) {
            city.setCityList(getCityMenu(city.getId()));
        }return list;
    }

    /**
     * 查询下一层树状城市信息
     */
    public List<City> getNextMenu(Long parentId){
        LambdaQueryWrapper<City> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(City::getParentId, parentId);
        List<City> list = cityService.list(queryWrapper);
        return list;
    }

    /**
     * 查询某节点开始的所有树状城市信息
     */
    public City getCity(Long id){
        City city = cityService.getById(id);
        city.setCityList(getCityMenu(id));
        return city;
    }

    /**
     * 查询某节点开始的下一层树状城市信息
     */
    @ApiOperation("根据城市id获取下一层城市信息")
    @GetMapping("/{id}")
    public City getNext(@PathVariable("id") Long id){
        City city = cityService.getById(id);
        city.setCityList(getNextMenu(id));
        return city;
    }

    /**
     * 查询遵义市及下一级的城市信息 2531
     */
    @ApiOperation("查询遵义市及下一层城市信息")
    @GetMapping
    public AjaxResult getMenu(){
        return AjaxResult.success(getNext(2531L));
    }
}
